<?php

namespace app\api\model;

use think\Db;
use think\Model;

class GameModel extends Model {
    function gamedetail($gameid) {
        $result = Db::name('game')->where('gameid', $gameid)->find();
        return $result;
    }

    function gamelist() {
        $result = Db::name('game')->select();
        return $result;
    }

    function gameenroll($gameid, $userid) {
        $data = [
            'userid' => $userid,
            'gameid' => $gameid,
        ];
        //判断用户存在
        $hasUser = Db::name('user')
            ->where('userid', $userid)
            ->select();

        if (!$hasUser) {
            return ['success' => -1, 'msg' => '无此用户'];
        }

        //判断比赛
        $hasGame = Db::name('game')
            ->where('gameid', $gameid)
            ->select();

        if (!$hasGame) {
            return ['success' => -1, 'msg' => '无此比赛'];
        }

        //判断报名
        $hasEnroll = Db::name('game_enroll')
            ->where('userid', $userid)
            ->where('gameid', $gameid)
            ->select();

        if ($hasEnroll) {
            return ['success' => -1, 'msg' => '已报名'];
        }

        $result = Db::name('game_enroll')->insert($data);
        if ($result) {
            return ['success' => $result];

        } else {
            return ['success' => -1,'msg'=>'报名失败'];
        }
    }

    function collect($userid, $gameid) {
        //判断用户存在
        $hasUser = db('user')
            ->where('userid', $userid)
            ->find();

        if (!$hasUser) {
            return ['success' => -1, 'msg' => '无此用户'];
        }

        //判断比赛
        $hasGame = Db::name('game')
            ->where('gameid', $gameid)
            ->select();

        if (!$hasGame) {
            return ['success' => -1, 'msg' => '无此比赛'];
        }

        //是否已经收藏
        $isCollect = db('game_follow')
            ->where('gameid', $gameid)
            ->where('userid', $userid)
            ->find();

        if ($isCollect) {
            $res = db('game_follow')
                ->where('gameid', $gameid)
                ->where('userid', $userid)
                ->delete();
            if ($res) {
                return ['success' => 0, 'msg'=>'取消收藏'];
            } else {
                return ['success' => -1, 'msg'=>'取消收藏失败'];
            }
        } else {
            $data = ['userid' => $userid, 'gameid' => $gameid];
            $res = db('game_follow')->insert($data);
            if ($res) {
                return ['success' => 1, 'msg'=>'收藏成功'];
            } else {
                return ['success' => -1,'msg'=> '收藏失败'];
            }
        }
    }

    function isCollect($userid, $gameid) {
        $res = db('game_follow')
            ->where('gameid', $gameid)
            ->where('userid', $userid)
            ->find();
        if ($res) {
            return ['success' => 1];
        } else {
            return ['success' => 0];
        }
    }

    function collectGameList($userid){
        $res = Db::table('game_follow')
            ->alias('follow')
            ->join('game',"follow.gameid=game.gameid and follow.userid = '$userid'")
            ->select();
        return $res;
    }
}